<!--待回款订单明细-->
<template>
  <div>
    <f-search-form ref='search' :more='true' @searchHandler='searchHandler'>
      <f-search-form-item label='订单编号' name='orderNo' />
      <f-search-form-item label='业务员' name='saleManName' />
      <f-search-form-item label='单位名称' name='customName' />
      <f-search-form-item label='部门' name='deptId' type='select' :data='api.hyorder.listByCompanyId' :params='queryParam' value-key='deptId' label-key='deptName' slot='advanced' />
      <f-search-form-item label='发货日期' type='range' name='beginDate' slot='advanced' />
    </f-search-form>
    <f-table ref='table' rowKey='orderDetailsReturnId' 
             :showSetting='false' setting-key='return_tab1'
             :tableColumns='columns' :scroll='{x:true}' 
             handler='hyReturnAmountTab1PageListHandler' :showMultipleBtn='false' :operator='operator' :multiple='multiple' :params='queryParam' :api='api.hyOrderDetailsReturn'>
       <span slot='saleType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text=="CHA"'>销售</a-tag>
           <a-tag color='#FF0000' v-if='text=="CHC"'>退货</a-tag>
           <a-tag color='purple' v-if='text==4'>票折</a-tag>
       </span>
      <span slot='sendType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text==1'>现款</a-tag>
           <a-tag color='#FF0000' v-if='text==2'>预收款</a-tag>
           <a-tag color='#87d068' v-if='text==3'>资保金</a-tag>
           <a-tag color='purple' v-if='text==4'>全额资信</a-tag>
       </span>
      <span slot='drugType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text=="ZY"'>自营</a-tag>
           <a-tag color='#FF0000' v-if='text=="ZS"'>招商</a-tag>
       </span>
      <span slot='action' slot-scope='text, record'>
          <template>
            <a-dropdown>
             <a class='ant-dropdown-link'>操作<a-icon type='down' /></a>
             <a-menu slot='overlay' v-if='record.sendState==1'> 
                <!--未退货的可拆分,已拆分的不可进行拆分-->
               <a-menu-item v-if='!record.parentId'>
                 <a @click='$refs.chaiModal.open(record)'>拆分</a>
               </a-menu-item>
              <a-menu-item v-if='record.saleType=="CHA"'>
                 <a @click='$refs.discountModal.open(record)'>票折</a>
               </a-menu-item>
               <!--                <a-menu-item v-if='record.saleType==4'>
                                 <a @click='delDiscount(record)'>删除票折</a>
                               </a-menu-item>-->
               <!--未回款或者已回款且已审批的可以退货-->
               <!--                <a-menu-item v-if='record.saleType=="CHA"'>
                                 <a @click='$refs.tuiModal.open(record)'>退货</a>
                               </a-menu-item>-->
               <a-menu-item v-if='record.parentId && record.saleType=="CHA"'>
                    <a @click='cancelChaiFen(record)'>撤销拆分</a>
                </a-menu-item>
                <a-menu-item>
                    <a @click='updateReturnState(record)'>标记为已回款</a>
                </a-menu-item>
             </a-menu>
            </a-dropdown>
          </template>
        </span>
    </f-table>
    <chai-modal ref='chaiModal' @success='onLoad'></chai-modal>
    <return-drawer ref='returnModal' @success='onLoad'></return-drawer>
    <discount-modal ref='discountModal' @success='onLoad'></discount-modal>
    <tui-modal ref='tuiModal' @success='onLoad'></tui-modal>
  </div>
</template>
<script>
import api from "../../js/api"
import ChaiModal from "@/views/module/haiying/order/return/components/chai_modal"
import ReturnDrawer from "@/views/module/haiying/order/return/components/return_drawer"
import DiscountModal from "@/views/module/haiying/order/return/components/discount_modal"
import TuiModal from "@/views/module/haiying/order/return/components/tui_modal"

export default {
  name: "Tab1",
  components: { TuiModal, DiscountModal, ReturnDrawer, ChaiModal },
  data() {
    let me = this
    //操作按钮
    let operator = []
    operator.push({
      name: "回款",
      that: this,
      handler: (table, that) => {
        let selectIds = table.selectedRowKeys
        if (selectIds.length > 0) {
          this.$refs.returnModal.showDrawer({
            selectIds: selectIds.join(",")
          })
        } else {
          that.$message.error("请选择回款数据")
        }
      }
    })
    return {
      api: Object.assign(api, this.api),
      multiple: true,
      deptId: "",
      //列表初始化查询参数
      queryParam: {
        auth: 1,
        isFlp: 1
      },
      returnCustomBond: 0,
      selectIds: "",
      params1: {
        deptId: ""
      },
      columns: [
        {
          title: "序号",
          width: "60px",
          align: "center",
          fixed: "left",
          dataIndex: "rowIndex",
          customRender: function(text, record, index) {
            return (record.rowIndex !== "合计") ? (parseInt(index) + 1) : record.rowIndex
          }
        },
        {
          title: "发(退)货日期",
          align: "center",
          width: "100px",
          fixed: "left",
          sorter: true,
          dataIndex: "sendDate"
        },

        {
          title: "订单编号",
          align: "center",
          fixed: "left",
          sorter: true,
          customCell: this.renderTdBackground,
          dataIndex: "orderNo"
        },
        {
          title: "出库单号",
          align: "center",
          fixed: "left",
          sorter: true,
          dataIndex: "outOrderNo"
        },
        {
          title: "发货方式",
          align: "left",
          width: "100px",
          dataIndex: "sendType",
          scopedSlots: { customRender: "sendType" }
        },
        {
          title: "单据类型",
          align: "center",
          width: "80px",
          dataIndex: "saleType",
          scopedSlots: { customRender: "saleType" }
        },
        {
          title: "商品类型",
          align: "center",
          width: "80px",
          dataIndex: "drugType",
          scopedSlots: { customRender: "drugType" },
          filters: [
            {
              text: "招商",
              value: "ZS"
            },
            {
              text: "自营",
              value: "ZY"
            }
          ],
          filterMultiple: false
        },
        {
          title: "业务员",
          align: "left",
          width: "100px",
          sorter: true,
          dataIndex: "saleManName"
        },
        {
          title: "业务员编码",
          align: "left",
          width: "100px",
          sorter: true,
          dataIndex: "businessCode"
        },
        {
          title: "部门名称",
          align: "left",
          width: "100px",
          sorter: true,
          dataIndex: "deptName"
        },
        {
          title: "单位名称",
          align: "left",
          width: "130px",
          sorter: true,
          ellipsis: true,
          dataIndex: "customName"
        },
        {
          title: "商品名称",
          align: "left",
          ellipsis: true,
          width: "140px",
          dataIndex: "drugsName"
        },
        {
          title: "规格",
          align: "left",
          width: "80px",
          ellipsis: true,
          dataIndex: "drugsSpec"
        },
        {
          title: "销售单价",
          align: "center",
          width: "80px",
          dataIndex: "salePrice"
        },
        {
          title: "数量",
          align: "center",
          width: "80px",
          dataIndex: "saleNum"
        },
        {
          title: "未回款金额",
          align: "center",
          needTotal: true,
          fixed: "right",
          dataIndex: "saleAmount"
        }, {
          title: "操作",
          align: "center",
          dataIndex: "action",
          width: "80px",
          fixed: "right",
          scopedSlots: { customRender: "action" }
        }
      ],
      operator: operator
    }
  },
  created() {
  },
  methods: {
    renderTdBackground(record, flag) {
      if (record.parentId) {
        return {
          style: {
            "background-color": "#C9CC3F"
          }
        }
      }
    },
    setData() {
    },
    //部门切换
    deptChange(value) {
      this.queryParam.deptId = value
      this.$refs.saleMan.initData(this.queryParam)
      this.$refs.table.refresh(true, this.queryParam)
      this.$forceUpdate()
    },
    /**
     * 删除商品
     */
    delDiscount(record, index) {
      this.http.confirm(this, {
        content: "确认删除吗?",
        url: api.hyOrderDetails.deleteById,
        data: {
          orderDetailsId: record.orderDetailsId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    /**
     * 撤销拆分
     */
    cancelChaiFen(order) {
      this.http.confirm(this, {
        content: "确认撤销拆分吗?",
        url: api.hyOrderDetailsReturn.cancelChaiFen,
        data: {
          orderDetailsReturnId: order.orderDetailsReturnId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    /**
     * 标记为已回款
     */
    updateReturnState(order) {
      this.http.confirm(this, {
        content: "确认标记为已回款吗?",
        url: api.hyOrderDetailsReturn.updateReturnState,
        data: {
          orderDetailsReturnId: order.orderDetailsReturnId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    /**
     * 查询事件
     * @param searchParams
     */
    searchTable(searchParams) {
      this.queryParam = Object.assign(this.queryParam, searchParams)
      this.$refs.table.refresh(true, searchParams)
    },
    /**
     * 刷新列表
     */
    onLoad() {
      this.$refs.table.refresh()
      this.$emit("changeTab")
    }
  }
}
</script>